/**
*文件用途说明：XpopDialog 弹窗
*依赖库：jQuery 1.4.4
*作者：徐国南
*制作日期： 2013-3-13
*修改日期： 2013-3-25 
					.改善弹出居中
					.添加拖拽
		    2013-5-2 
                    .增加宽度重置接口
                    .增加标题重置接口
			2013-6-28 
                    .修复谷歌弹窗隐藏、闪烁bug 
                    .优化ie6遮罩     
					++++++++++++++++++++++++++++++++++++++++++++++++++++++
					遗留问题，浏览器判断  chrome  ie6 是否充分//待解决，待优化
					++++++++++++++++++++++++++++++++++++++++++++++++++++++
			2013-7-17 
                    .增加 fixed 定位接口
                    .整理细节优化
					++++++++++++++++++++++++++++++++++++++++++++++++++++++
					遗留问题，优化ie6下定位方式/待优化
					++++++++++++++++++++++++++++++++++++++++++++++++++++++
			2013-8-30
                    .增加 fixed 定位接口
                    .整理细节优化
					++++++++++++++++++++++++++++++++++++++++++++++++++++++
					遗留问题，当弹出层是多层时，遮罩层 z-index 问题
					++++++++++++++++++++++++++++++++++++++++++++++++++++++
			2013-9-23 
					.增加popType 接口
			2013-9-30 
					.在popType 接口 添加 fixed, nearby, absolute
					主要优化了 nearby 定位
					++++++++++++++++++++++++++++++++++++++++++++++++++++++
					遗留问题  popType 接口 nearby  未给出位置参数时，需要默认定位参数
					遗留问题 继续优化 popType 接口
					++++++++++++++++++++++++++++++++++++++++++++++++++++++
**/

;(function($){
    "use strict";
	$.extend({
		"XpopDialog":function(opt){
			var options=$.extend({
				id:"",
				title:"",
				popType:['absolute',500,500], // fixed, nearby, absolute
				width:"300",
				mask:true,
				status:false,
				content:"",
				closeable:true,
				draggable:true,
				fixed:false
			},opt),
			//弹窗实例
			instance,
			isIE6 = !-[1,] && !window.XMLHttpRequest,
			level;

			function Obj(){
				this.id = options.id;
				this.title = options.title;
				this.width = options.width;
				this.status = options.status;
				this.mask = options.mask;
				this.mask_id = "mask_" + this.id;
				this.content = options.content;
				this.closeable = options.closeable;
				this.draggable = options.draggable;
				this.fixed = options.fixed;
				this.popType = options.popType;
				
				//初始化
				$("body").append(this.makeHTML());
				//this.makeHTML().appendTo("body");

				this.el = $("#" + this.id);
				this.el.width(this.width);
				this.close_el= this.el.find(".hd .close");
				
				//生成遮罩
				if(this.mask === true){
					$("body").append(this.makeMask());
				}
				this.mask_el= $("#" + this.mask_id);

				//如果参数配置closeable为false 则禁止关闭
				if(this.closeable===false){
					this.close_el.addClass("disable hide");
				}else{
					this.close_el.removeClass("disable");
					this.el.delegate(".close","click",$.proxy(this.hidePop,this));
				}

				//---------------------
				// Directions	: 存储触发弹窗的目标元素
				//---------------------
				this.e = null;
			}

			Obj.prototype ={
			    //private
				makeHTML:function(){
					var htmlstr ="";
						htmlstr+='<div class="XpopDialog" id="' + this.id + '">';
							htmlstr+='<div class="hd">';
								htmlstr+='<h4 class="tit">' + this.title + '</h4>';
								htmlstr+='<a class="close" href="javascript:void(0);">关闭</a>';
							htmlstr+='</div>';
							htmlstr+='<div class="bd">';
							htmlstr+='</div>';
						htmlstr+='</div>';
						return htmlstr;
				},
				//private
				makeMask:function(){
					var htmlstr ='<div class="XpopDialog_mask" id="' + this.mask_id + '"></div>';
					return htmlstr;
				},
				//private 定位 (closure)
				pos:function(e){
					//var $target=$(e.target),
					var	window_width = $(window).width(),
						window_height = $(window).height(),
						window_scroll_top = $(window).scrollTop(),
						window_scroll_left = $(window).scrollLeft(),
						//target_positon_top = $target.position().top,
						//target_offset_top = $target.offset().top,
						//target_scroll_top = $target.scrollTop(),
						//target_scroll_left = $target.scrollLeft(),
						//target_current_scroll_top = target_offset_top - window_scroll_top,
						width = this.width,
						height = this.el.height();

						switch(this.popType[0]){
							case "absolute":
								//console.log("absolute");
								if(window.navigator.userAgent.indexOf("Chrome") !== -1 ){
									this.pop = function(){
										//位置
										this.el.css({
											"position":"absolute",
											top:window_scroll_top + (window_height/8) + 'px',
											left:window_scroll_left + window_width/2 - width/2 + 'px'
										});
										$("html,body").animate({scrollTop:window_scroll_top - 1},0).animate({scrollTop:window_scroll_top + 1},0);
										//执行拖拽
										if(this.draggable === true){
											this.el.addClass("draggable");
											this.dragDrop().enable();
										}
									};
								}else{
									this.pop = function(){
										//位置
										this.el.css({top:window_scroll_top + (window_height/8) + 'px'});
										this.el.css({left:window_scroll_left + window_width/2 - width/2 + 'px'});
									};
								};
								break;
							case "nearby":
								this.pop = function(e){
									//console.log(1)
									var $target = $(e.target);
									var $position = $target.position();
									var top = options.popType[1];
									var left = options.popType[2];
									//offset()
									//this.el.scrollTop($target.scrollTop()); 
									this.el.css({
										top :($target.offset().top + top) + "px",
										left: ($target.offset().left  + left ) + "px"
									})
								}
								break;
							case "fixed":
								//console.log("fixed");
								if(isIE6){
									this.pop = function(){
										//alert("ie6不支持fixed定位");
										this.el.css({
											"position":"absolute",
											"left" : 50 + "%",
											//"top"  : 10 + "%",
											"top" : $(window).scrollTop() + 10 + "px",
											"marginLeft" : -(width/2) + "px"
										});
										el = this.el;

										$(window).bind("scroll", function(){ 
											//当滚动条滚动时
											//alert("scrolled");
											el.css({
												"top" : $(window).scrollTop() + 10 + "px"
											})
										}); 
									}
								}else{
									this.pop = function(){
										this.el.css({
											"position":"fixed",
											"left" : 50 + "%",
											"top"  : 10 + "%",
											"marginLeft" : -(width/2) + "px"
										});
										this.el.children(".hd").css({
											cursor:"default"
										});
									};
								}
						}
						
						//执行拖拽
						if(this.draggable === true){
							this.el.addClass("draggable");
							this.dragDrop().enable();
						}
					return this.pop(e);
				},
				//private 拖拽
				dragDrop:function(){
					var self = this,
						hd = self.el.children(".hd"),
						dragging = null,
						diffX = 0,
						diffY = 0;
					function handleEvent(event){
						var $target= self.el;
						switch(event.type)
						{
							case "mousedown":
								dragging = $target;
								diffX = event.pageX - $target.offset().left;
								diffY = event.pageY - $target.offset().top;
							break;
							case "mousemove":
								if(dragging !== null){
									dragging.css({"margin-left":0,left:(event.pageX - diffX) + "px",top:(event.pageY - diffY)+ "px"});
								}
							break;
							case "mouseup":
								dragging = null;
							break;
							default:
						}
					}
					return {
						enable:function(){
							hd.bind("mousedown",handleEvent);
							$("html,body").bind("mousemove",handleEvent);
							$("html,body").bind("mouseup",handleEvent);
						},
						disable:function(){
							hd.unbind("mousedown",handleEvent);
							$("html,body").unbind("mousemove",handleEvent);
							$("html,body").unbind("mouseup",handleEvent);
						}
					};
				},
				//public 设置标题
                setTitle:function(str){
                    this.title = str;
                    this.el.find('.hd .tit').html(this.title);
                },
				//public 设置内容
				setContent:function(str){
					this.content = str;
					this.el.find(".bd").html(this.content);
				},
				//public 设置宽度
                setWidth:function(num){
                    this.width = num;
                    this.el.width(this.width);
                    if(this.e){
                        this.pos(this.e); 
                    }
                },
				//public 显示
				showPop:function(e){
					//this.e = e;
					var mask_el = this.mask_el;

					if (isIE6) {
						this.showPop = function(e){
							if(this.status===false){
								this.el.show();
								this.status=true;
							}else{
								this.el.show();
							}
							//显示遮罩
							if(this.mask === true){
								mask_el.show();
							}
							
							mask_el.css("top",$(window).scrollTop());
							$(window).bind("scroll", function(){ 
								//当滚动条滚动时
								mask_el.css("top",$(window).scrollTop());
							}); 
							this.pos(e);
						}; 
					}else{
						this.showPop = function(e){
							if(this.status===false){
								this.el.show();
								this.status=true;
							}else{
								this.el.show();
							}
							//显示遮罩
							if(this.mask === true){
								this.mask_el.show();
							}
							this.pos(e);
						};
					}
					return this.showPop(e);
				},
				//public 隐藏
				hidePop:function(){
					this.el.hide();
					this.mask_el.hide();
					$(window).unbind("scroll", function(){ 
						//当滚动条滚动时
						this.mask_el.css("top",$(window).scrollTop());
					}); 
				}
			};

			//当触发事件实例化对象
			instance=new Obj();
			return instance;
		}
	});
}(jQuery));